﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gacn.ControleAlunos.Library.Business;
using System.Collections.Specialized;
using System.Data;
using Gacn.ControleAlunos.Library.Entities;

namespace Gacn.ControleAlunos.WebApplication
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ddlDatas.Items.Count == 1 || ddlEscolas.Items.Count == 1)
            {
                FillDdlEscolas();
                FillDdlDatas();
            }
        }

        #region Fill dos Dados
        private void FillDdlEscolas()
        {
            List<EscolaEntity> escolas = EscolaBiz.GetAllEscolaEntity();
            foreach (EscolaEntity escola in escolas)
                ddlEscolas.Items.Add(new ListItem(escola.Nome, escola.ID.ToString()));
        }

        private void FillGvParaisopolis()
        {
            OrderedDictionary od = RelatorioBiz.QtdePresencaDasTurmasPorData(DateTime.Today);
            DataTable dt = new DataTable();
            dt.Columns.Add("turmas");
            dt.Columns.Add("presenças");

            foreach (string key in od.Keys)
                dt.Rows.Add(key, od[key]);

            gvParaisopolis.DataSource = dt;
            gvParaisopolis.DataBind();
            gvParaisopolis.Columns[0].SortExpression = "Turmas";
        }

        private void FillDdlDatas()
        {
            List<DateTime> datas = RelatorioBiz.DatasDeAulaDoAno(DateTime.Today.Year);
            
            foreach(DateTime data in datas)
                ddlDatas.Items.Add(new ListItem(data.Day + "/" + data.Month + "/" + data.Year, data.Date.ToString()));
        }

        #endregion

        protected void Pesquisar_Click(object sender, EventArgs e)
        {
            if (ddlEscolas.SelectedIndex != 0 && ddlDatas.SelectedIndex != 0)
            {
                PesquisarPorEscolaPorEscola();
                lblPresencasParaisopolis.Text = Convert.ToString(RelatorioBiz.QtdePresencaDaDataPorEscola(
                    DateTime.Parse(ddlDatas.SelectedValue.ToString()),
                    int.Parse(ddlEscolas.SelectedValue.ToString())));
            }
        }

        private void PesquisarPorEscolaPorEscola()
        {
            SortedDictionary<string, int> sd = RelatorioBiz.PresencaDasTurmasPorDataPorEscola(
                DateTime.Parse(ddlDatas.SelectedValue),int.Parse(ddlEscolas.SelectedValue.ToString()));

            DataTable dt = new DataTable();
            dt.Columns.Add("turmas");
            dt.Columns.Add("presenças");

            foreach (string key in sd.Keys)
                dt.Rows.Add(key, sd[key]);

            gvParaisopolis.DataSource = dt;
            gvParaisopolis.DataBind();
            gvParaisopolis.Columns[0].SortExpression = "Turmas";
        }
    }
}